import 'dart:convert';

import 'package:body_measurement/common/measuring_api.dart';
import 'package:body_measurement/models/user_info.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Global {
  static late SharedPreferences _prefs;
  static String? token;

  static Future init() async {
    WidgetsFlutterBinding.ensureInitialized();
    _prefs = await SharedPreferences.getInstance();
    var _profile = _prefs.getString('profile');
    if (_profile != null) {}

    MeasuringApi().getToken();
  }

  static UserInfo? get userInfo {
    if (!_prefs.containsKey('userInfo')) {
      return null;
    }

    return UserInfo.fromJson(jsonDecode(_prefs.getString('userInfo')!));
  }

  static set userInfo(info) => _prefs.setString('userInfo', jsonEncode(info.toJson()));
}
